<template>
  <div class="flowNodeAdd">
    <el-form ref="nodeForm" :model="node" label-width="80px" :rules="rules">
      <el-form-item label="节点名" prop="name">
        <el-input v-model="node.name"></el-input>
      </el-form-item>
      <el-form-item prop="type" label="通过类型" label-width="80px">
        <el-select v-model="node.type">
          <el-option
            v-for="item in types"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="backType" label="退回类型" label-width="80px">
        <el-select v-model="node.backType">
          <el-option
            v-for="item in backTypes"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" autosize v-model="node.remark"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="save" type="primary"
          ><span v-if="config.isChange">修改</span
          ><span v-else>添加</span></el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "FlowNodeAdd",
  props: ["isChange", "fNode" , 'fFlowId'],
  data() {
    return {
      node: {
        id: null,
        name: "",
        flowId: null,
        remark: "",
        type: '',
        backType: ''
      },
      config: {
        isChange: false
      },
      types:[],
      backTypes:[],
      rules: {
        name: [{ required: true, message: "请输入节点名", trigger: "blur" }],
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        type: [{ required: true, message: "请选择提交类型", trigger: "blur" }],
        backType: [{ required: true, message: "请选择退回类型", trigger: "blur" }]
      }
    };
  },
  watch: {
    isChange(val) {
      this.config.isChange = this.isChange;
      //给类型下拉框添加默认值
    },
    fFlowId(val){
      this.flowId = this.fFlowId;
    },
    fNode(val) {
      if (this.fNode == null) {
        this.node = {
        id: null,
        name: "",
        flowId: this.flowId,
        remark: "",
        type: null,
        backType: null
        };
        //给类型下拉框添加默认值
      } else {
        this.node = JSON.parse(JSON.stringify(this.fNode));
      }
    }
  },
  created() {
    this.flowId = this.fFlowId;
    if(this.flowId == null){
      this.$message.error("数据异常");
    }
    if (this.fNode == null) {
        this.node = {
        id: null,
        name: "",
        flowId: this.flowId,
        remark: "",
        type: null,
        backType: null
        };
    } else {
      this.node = JSON.parse(JSON.stringify(this.fNode));
    }
    this.config.isChange = this.isChange;
    this.dataInit();
    //给类型下拉框添加默认值
  },
  methods: {
    dataInit(){
      //调用服务
      var that = this;
      this.$axios
        .get("node/getAllNodeType")
        .then(res => {
          let data = res.data;
          if (data.code === 0) {
            this.types = data.data;
          } else {
            this.$message.error(data.msg);
          }
        })
        .catch(function(error) {
          console.log("axios请求异常:" + error);
          this.$message.error("网路请求异常");
        });
      //调用服务
      this.$axios
        .get("node/getAllNodeBackType")
        .then(res => {
          let data = res.data;
          if (data.code === 0) {
            this.backTypes = data.data;
          } else {
            this.$message.error(data.msg);
          }
        })
        .catch(function(error) {
          console.log("axios请求异常:" + error);
          that.$message.error("网路请求异常");
        });
    }
    ,save() {
      //调用服务
      var that = this;
      this.$refs["nodeForm"].validate(valid => {
        if (valid) {
          this.$axios
            .get("node/save", {
              params: this.node
            })
            .then(res => {
              let data = res.data;
              if (data.code === 0) {
                this.$message({ message: data.msg, type: "success" });
                this.$emit("input", false);
                this.$emit("flush-data");
              } else {
                this.$message.error(data.msg);
              }
            })
            .catch(function(error) {
              console.log("axios请求异常:" + error);
              that.$message.error("网路请求异常");
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
</style>
